﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_dangthongbao : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CKEditorControl1.Language = "vi";
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "../ckfinder/";
            _FileBrowser.SetupCKEditor(CKEditorControl1);
            danhsach(0);
            //GridView2.DataBind();
        }
    }
    protected void butOK_Click(object sender, EventArgs e)
    {
        //them thong bao
        clsTableAction clsTable = new clsTableAction("ThongBao");
        string str = string.Format("select * from ThongBao where MaTB='" + TextBox1.Text + "'");
        //string str = string.Format("select * from ThongBao");
        bool kq = false;
        kq = clsTable.ktrunSQL(str);
        if (kq == false)
        {
            Label1.Text = "ma thong bao da ton tai";
            TextBox1.Text = "";
            return;
        }
        else
        {
            string file = txtUrl.Text;
            string sql = string.Format("insert into thongbao values('{0}','{1}',N'{2}','{3}',N'{4}','{5}');", TextBox1.Text, TextBox2.Text, CKEditorControl1.Text, DateTime.Now, TextBox4.Text, file);

            if (clsTable.runSQL(sql))
            {
                GridView1.EditIndex = -1;
                danhsach(0);
                Label1.Text = "Tin bạn đăng thành công";
                TextBox1.Text = "";
                TextBox2.Text = "";
                CKEditorControl1.Text = "";
                TextBox4.Text = "";

            }
            else { Label1.Text = "Tin bạn đăng khong thành công" + kq; }
        }
    }
    protected void butXoa_Click(object sender, EventArgs e)
    {
        clsQueryList clsQuery = new clsQueryList();
        //clsTableAction clsTable = new clsTableAction("ThongBao");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[5].FindControl("chkXoa");
            if (chk.Checked)
            {
                string id = GridView1.Rows[i].Cells[0].Text;
                //Response.Write(id);
                //Tien hanh xoa
                string sql = "delete from ThongBao where MaTB='" + id + "'";
                //string sql = string.Format("delete from thongbao where MaTB=''", id);
                clsQuery.addSQL(sql);
            }
        }
        if (clsQuery.Execute())
        {
            danhsach(GridView1.PageIndex);
            tbl.Text = ("ban da xoa thanh cong");
        }
        else
            tbl.Text = ("xoa that bai");
    }
    protected void danhsach(int index)
    {
        clsTableAction clsTable = new clsTableAction("ThongBao");
        clsTable.exportDataToGridview("*", "", ref GridView1, 5, index);
        //clsTableAction clsTable = new clsTableAction("ThongBao");
        //clsTable.exportDataToGridviewPaging(0, 5, ref GridView2, "*", "");
        //clsTable.free();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        danhsach(e.NewPageIndex);
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        danhsach(GridView1.PageIndex);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        danhsach(GridView1.PageIndex);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        clsTableAction clsTable = new clsTableAction("ThongBao");
        string id = GridView1.Rows[GridView1.EditIndex].Cells[0].Text;
        TextBox txt1 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[1].Controls[0];
        string lableVi2 = txt1.Text;
        TextBox txt2 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[2].Controls[0];
        string lableVi3 = txt2.Text;
        TextBox txt3 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[3].Controls[0];
        string lableVi4 = txt3.Text;
        TextBox txt4 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[4].Controls[0];
        string lableVi5 = txt4.Text;

        //Tien hanh update
        string sql = string.Format("update ThongBao set TieuDe_TB='{0}', NoiDung_TB='{1}', NgayTB ='{2}', MaNV = '{3}', FlieKem='{4}' where MaTB='{5}'", txt1.Text, txt2.Text, DateTime.Now, txt3.Text, txt4.Text, id);
        if (clsTable.runSQL(sql))
        {
            GridView1.EditIndex = -1;
            danhsach(GridView1.PageIndex);
        }
        else { tbl.Text = "that bai"; }
        clsTable.free();
    }
    //protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    clsTableAction clsTable = new clsTableAction("ThongBao");
    //    int kq = 0;
    //    string id = GridView2.Rows[e.RowIndex].Cells[0].Text;
    //    if (clsTable.delete("MaTB =" + id, out kq) && kq != 0)
    //    {

    //    }
    //}
    private Boolean uploadImage(FileUpload FileImage, string fileName)
    {
        try
        {
            if (FileImage.HasFile)
            {
                string path = Server.MapPath("../") + "/Files/" + fileName;
                FileImage.SaveAs(path);
                //clsImages clsImg = new clsImages();
                //clsImg.saveAndResizeImage(path, path, 115, 79, ".jpg");
                return true;
            }
            return false;
        }
        catch
        {
            return false;
        }
    }
}